<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <input type="file">
  <button>上传文件</button>
  <img src="" alt="">

  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    // http://www.liulongbin.top:3006/api/upload/avatar
    let file = document.querySelector('input')
    let btn = document.querySelector('button')
    btn.addEventListener('click', function () {
      // files[0] 其实就是文件那个对象
      console.dir(file.files[0]);
      let formData = new FormData()

      formData.append('avatar', file.files[0])

      axios.post('http://www.liulongbin.top:3006/api/upload/avatar', formData)
        .then(res => {
          let url = 'http://www.liulongbin.top:3006' + res.data.url
          let img = document.querySelector('img')
          img.src = url
        })
    })
  </script>
</body>
</html>